package cn.heygod.graywhale.modules.ipaas.controller;

import com.github.pagehelper.PageInfo;
import cn.heygod.graywhale.common.context.UserContext;
import cn.heygod.graywhale.modules.ipaas.domain.GwApiReq;
import cn.heygod.graywhale.common.tools.string.Convert;
import cn.heygod.graywhale.common.web.base.BaseController;
import cn.heygod.graywhale.common.web.domain.request.PageDomain;
import cn.heygod.graywhale.common.web.domain.response.Result;
import cn.heygod.graywhale.common.web.domain.response.module.ResultTable;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import cn.heygod.graywhale.modules.ipaas.service.IGwApiReqService;

import java.time.LocalDateTime;
import java.util.Arrays;

/**
 * API需求Controller
 *
 * @author simon
 * @date 2023-12-05
 */
@RestController
@RequestMapping("/ipaas/apiReq")
public class GwApiReqController extends BaseController {

    private String prefix = "ipaas/apiReq";

    @Autowired
    private IGwApiReqService gwApiReqService;

    @GetMapping("/main")
    @PreAuthorize("hasPermission('/ipaas/apiReq/main','ipaas:apiReq:main')")
    public ModelAndView main() {
        return jumpPage(prefix + "/main");
    }

    /**
     * 查询API需求列表
     */
    @ResponseBody
    @GetMapping("/data")
    @PreAuthorize("hasPermission('/ipaas/apiReq/data','ipaas:apiReq:data')")
    public ResultTable list(@ModelAttribute GwApiReq gwApiReq, PageDomain pageDomain) {
        PageInfo<GwApiReq> pageInfo = gwApiReqService.selectGwApiReqPage(gwApiReq, pageDomain);
        return pageTable(pageInfo.getList(), pageInfo.getTotal());
    }

    /**
     * 新增API需求
     */
    @GetMapping("/add")
    @PreAuthorize("hasPermission('/ipaas/apiReq/add','ipaas:apiReq:add')")
    public ModelAndView add() {
        return jumpPage(prefix + "/add");
    }

    /**
     * 新增API需求
     */
    @ResponseBody
    @PostMapping("/save")
    @PreAuthorize("hasPermission('/ipaas/apiReq/add','ipaas:apiReq:add')")
    public Result save(@RequestBody GwApiReq gwApiReq) {
        return decide(gwApiReqService.save(gwApiReq));
    }

    /**
     * 修改API需求
     */
    @GetMapping("/edit")
    @PreAuthorize("hasPermission('/ipaas/apiReq/edit','ipaas:apiReq:edit')")
    public ModelAndView edit(String id, ModelMap map) {
        GwApiReq gwApiReq =gwApiReqService.getById(id);
        map.put("gwApiReq", gwApiReq);
        return jumpPage(prefix + "/edit");
    }

    /**
     * 修改API需求
     */
    @ResponseBody
    @PutMapping("/update")
    @PreAuthorize("hasPermission('/ipaas/apiReq/edit','ipaas:apiReq:edit')")
    public Result update(@RequestBody GwApiReq gwApiReq) {
        return decide(gwApiReqService.updateById(gwApiReq));
    }

    /**
     * 删除API需求
     */
    @ResponseBody
    @DeleteMapping("/batchRemove")
    @PreAuthorize("hasPermission('/ipaas/apiReq/remove','ipaas:apiReq:remove')")
    public Result batchRemove(String ids) {
        return decide(gwApiReqService.removeByIds(Arrays.asList(ids.split(","))));
    }

    /**
     * 删除API需求
     */
    @ResponseBody
    @DeleteMapping("/remove/{id}")
    @PreAuthorize("hasPermission('/ipaas/apiReq/remove','ipaas:apiReq:remove')")
    public Result remove(@PathVariable("id") String id) {
        return decide(gwApiReqService.removeById(id));
    }
}
